package com.study.test.enumtest;

/**
 * 枚举类学习，获取key获取value方法
 * Created by tao on 2018/7/28.
 */
public enum Color {
    RED(1, "红色"),
    GREE(2, "绿色");

    private Integer key;
    private String name;

    Color() {

    }

    Color(Integer key, String name) {
        this.key = key;
        this.name = name;
    }

    public Integer getKey() {
        return key;
    }

    public void setKey(Integer key) {
        this.key = key;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static Color getColorByKey(Integer key) {
        for (Color color : Color.values()) {
            if (color.key.equals(key)) {
                return color;
            }

        }
        return null;
    }
    public static Color getColorByName(String name) {
        for (Color color : Color.values()) {
            if (color.name.equals(name)) {
                return color;
            }

        }
        return null;
    }
    public static String getColorNameByKey(Integer key) {
        Color color = getColorByKey(key);
        if (color != null) {
           return color.getName();

        }
        return null;
    }
    public static Integer getColorKeyByName(String name) {
        Color color = getColorByName(name);
        if (color != null) {
            return color.getKey();

        }
        return null;
    }

    public static void main(String[] args) {
        String name = getColorNameByKey(1);
        System.out.println(name);
        String name2 = getColorNameByKey(4);
        System.out.println(name2);
        Integer key = getColorKeyByName("红色");
        System.out.println(key);
    }
}
